 <template>
  <div>
    <el-dialog
      title="编辑价格"
      width="40%"
      :visible.sync="dialogVisible"
      :before-close="handleClose"
    >
      <el-input v-model="newCategory" placeholder="添加新的价格">
        <el-button @click="addPrice" slot="append">添加</el-button>
      </el-input>

      <div style="width:100%;" class="list-style" v-for="(item, index) in category" :key="index">
        <span v-if="!item.isEdit">{{item.name}}</span>
        <el-input v-else v-model="item.newName" style="width:330px;">
          <template slot="append">
            <el-button @click="changePrice(item, index)" icon="el-icon-check"></el-button>
          </template>
        </el-input>
        <div v-if="!item.isEdit">
          <el-button
            @click="item.isEdit = true"
            size="mini"
            type="primary" class="add-style"
            icon="el-icon-edit"
            circle
          ></el-button>
          <el-button
            size="mini"
            type="danger"
            @click="removeCategory(item.id)"
            icon="el-icon-close"
            circle
          ></el-button>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { creatPrice, getPrice, deletePrice, updatePrice } from "api/course";
import { uploadImageUrl } from "../../../config/env.js";
export default {
  data() {
    return {
      category: [], //分类选项
      dialogVisible: this.showVisible,
      newCategory: "",
      chooseId: this.checkId
    };
  },
  props: ["checkId", "showVisible"],
  mounted() {
    this.getPrice();
  },
  watch: {
    showVisible: {
      handler: function(newVal, oldVal) {
        this.dialogVisible = newVal;
      }
    }
  },
  methods: {
    handleClose() {
      this.dialogVisible = false;
    },
    removeCategory(id) {
      this.$confirm("确认删除分类么？")
        .then(_ => {
          deletePrice(id).then(res => {
            this.$message.success("删除成功");
            this.getPrice();
          });
        })
        .catch(_ => {});
    },
    addPrice() {
      let newCategory = this.newCategory;
      if (!newCategory) return;
      creatPrice(newCategory).then(res => {
        this.$message.success("添加成功");
        this.newCategory = "";
        this.getPrice();
      });
    },
    changePrice(item, index) {
      if (item.name === item.newName) {
        this.category[index].isEdit = false;
      } else {
        updatePrice(item.id, item.newName).then(res => {
          this.$message.success("修改成功");
          this.getPrice();
        });
      }
    },
    getPrice() {
      getPrice().then(res => {
        this.category = res.map(item => {
          item.isEdit = false;
          item.checked = false;
          item.newName = item.name;
          return item;
        });
      });
    }
  }
};
</script>

<style lang="scss" scoped>
@import "src/style/mixin";

// ----------------编辑分类-------------------
.list-container {
  margin: 10px auto;
  width: 250px;
}
.list-style {
  @include fj(space-between);
  align-items: center;
  width: 200px;
  height: 50px;
  line-height: 50px;
  padding: 10px;
  border-radius: 10px;
  margin-top: 10px;
}
.list-style:hover {
  background: #f1f1f1;
}
// ----------------编辑分类-------------------
</style>
